package com.sbtr.controller.classify;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sbtr.business.classify.dto.AlgoVersionFileDto;
import com.sbtr.business.classify.entity.AlgoTemplateCondition;
import com.sbtr.business.classify.entity.AlgoVersionFile;
import com.sbtr.business.classify.service.AlgoTemplateConditionService;
import com.sbtr.business.classify.service.AlgoVersionFileService;
import com.sbtr.business.classify.vo.AlgoVersionFileVO;
import com.sbtr.common.PageDto;
import com.sbtr.common.Response;
import com.sbtr.common.file.FileService;
import com.sbtr.common.file.dto.FileDto;
import com.sbtr.controller.common.CommonController;
import com.sbtr.exception.BDException;
import com.sbtr.util.BeanCopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 算法版本字段列表 前端控制器
 *
 * @author dgl
 * @since 2023-06-13
 */
@RestController
@RequestMapping("/algoVersionFile")
@Api(tags = "算法版本字段列表")
public class AlgoVersionFileController extends CommonController<AlgoVersionFile, AlgoVersionFileService> {

    @Autowired
    FileService fileService;

    @Autowired
    AlgoTemplateConditionService algoTemplateConditionService;

    @GetMapping("/pageList")
    @ApiOperation(httpMethod = "GET", value = "分页列表")
    public Response<PageDto<AlgoVersionFile>> pageList(AlgoVersionFileDto algoVersionFileDto) {
        return Response.success(u.pageList(algoVersionFileDto));
    }

    @PostMapping("/save")
    @ApiOperation(httpMethod = "POST", value = "保存")
    public Response<AlgoVersionFile> save(@RequestBody AlgoVersionFile algoVersionFile) {
        algoVersionFile.setId(null);
        if (algoVersionFile.insert()) {
            return Response.success(algoVersionFile);
        } else throw new BDException("保存失败");
    }

    @PostMapping("download")
    @ApiOperation(httpMethod = "POST", value = "下载")
    public void download(String id, HttpServletResponse resp) {
        AlgoVersionFile algoVersionFile=u.download(id);
        fileService.downloadFile(resp, algoVersionFile.getFileUrl(), algoVersionFile.getFileName());
    }
    @PostMapping("upload")
    @ApiOperation(httpMethod = "POST", value = "上传")
    public Response<AlgoVersionFile> upload(AlgoVersionFileVO algoVersionFileVO, @RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request){
        AlgoVersionFile algoVersionFile=BeanCopyUtils.copyProperties(algoVersionFileVO,AlgoVersionFile.class);
        FileDto fileDto = fileService.uploadFile(file,new FileDto("algo"));
        algoVersionFile.setFileUrl(fileDto.getPath());
        algoVersionFile.setFileName(fileDto.getFileName());
        algoVersionFile.setUpdateTime(new Date());
        algoVersionFile.setCounts(0);
        algoVersionFile.setFileSize(fileDto.getFileSize());
        u.saveOrUpdate(algoVersionFile);
        return Response.success(algoVersionFile);
    }
    @PostMapping("/deleteByIds")
    @ApiOperation(httpMethod = "POST", value = "通用接口---根据id批量删除,逗号分隔ps:'1,2,3'，关联子表删除请重写")
    public Response<String> deleteByIds(String ids){
        List<String> list = Arrays.asList(ids.split(","));
        if(!u.removeByIds(list)){
            throw new BDException("删除失败");
        }

        UpdateWrapper<AlgoTemplateCondition> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("algo_template_version_id",list);
        algoTemplateConditionService.remove(updateWrapper);

        return Response.success("删除成功");
    }
}